package com.datastructure.test.oddbeforeeven;

public class OddBeforeEven {

    public static void main(String[] args) {
        OddBeforeEven oddBeforeEven = new OddBeforeEven();
        int[] data = {1,3,5,6,7};
        int[] ints = oddBeforeEven.reOrderArray(data);
        System.out.println(ints);
    }

    public int[] reOrderArray (int[] array) {
        // write code here
        if(array==null||array.length==0){
            return array;
        }
        int numOfEven = 0;
        for (int i = 0; i < array.length; i++) {
            if(array[i]%2==0){
                numOfEven++;
            } else if(array[i]%2==1&&numOfEven==0){
            } else {
                int time = i;
                for (int j = 0; j < numOfEven; j++,time--) {
                    swap(array,time,time-1);
                }
            }
        }
        return array;
    }

    public void swap(int[] array,int left,int right){
        int temp = array[left];
        array[left] = array[right];
        array[right] = temp;
    }
}
